<template>
    <div>
        <Tree :data="permissionData" show-checkbox ref="tree"></Tree>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                permissionData: [],
                permissions: []
            }
        },
        props: ['roleId'],
        methods: {
            getPermissionData(roleId) {
                var self = this;

                self.$http.get('/core/rolePermission/queryPermissionInfoTree', {
                    params: {
                        roleId: roleId
                    }
                }).then(function (response) {
                    var data = self.common.handleData(response.data, self);
                    self.permissionData = data;
                });

            },
            savePermissionData(roleId) {
                var self = this;

                self.getAllId();

                this.$http({
                    headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
                    transformRequest: [data => self.$qs.stringify(data)],
                    method: 'post',
                    url: '/core/rolePermission/addPermission',
                    data: {
                        roleId: roleId,
                        permission: self.permissions.toString()
                    }
                }).then(function (response) {

                }).catch(function (error) {
                    console.log(error);
                });


            },
            getAllId() {
                var self = this;
                self.permissions = [];
                let nodes = self.$refs.tree.getCheckedNodes();
                var flatState = this.$refs.tree.flatState;
                for (var i1 = 0, le1 = flatState.length; i1 < le1; i1++) {
                    for (var i = 0, le = nodes.length; i < le; i++) {
                        if (nodes[i].id == flatState[i1].node.id) {
                            self.permissions.push(flatState[i1].node.id);
                            if (flatState[i1].parent != undefined) {
                                self.getNodeChainId(flatState[i1].parent);
                            }
                        }
                    }
                }
            },
            getNodeChainId(index) {
                var self = this;
                var flatState = this.$refs.tree.flatState;
                var node = flatState[index];
                if (!self.common.isInArray(self.permissions, node.node.id)) {
                    self.permissions.push(node.node.id);
                    if (node.parent != undefined) {
                        self.getNodeChainId(node.parent);
                    }
                }
            }
        },

    }
</script>

<style scoped>

</style>